package org.ccay.message.service.impl;

import javax.inject.Inject;
import javax.inject.Named;

import org.ccay.core.exception.ApplicationException;
import org.ccay.core.request.impl.RequestContext;
import org.ccay.message.IPushMessageHandler;
import org.ccay.message.PushMessageArgs;
import org.ccay.message.PushMessageVO;
import org.ccay.message.service.IPushMessageService;
import org.ccay.registry.util.RegistryQueryUtil;

/**
 * 我的消息上 推送未读的消息个数 包含了过期的消息
 * Created：2013-8-27
 * @author chaowangbang
 * @version:
 */
@Named
public class PushNewMessageCount implements IPushMessageHandler {


	@Inject
	private IPushMessageService pushMessageService;
	
	public static final String CODE_FALSE = "0";
	
	// 消息类型
	public static final String messageType = "newMessageCount";

	public void execute(Object sender, PushMessageArgs args) throws ApplicationException {
		// 消息推送全局开关开启和具体消息类型的开关开启才发送消
		if (!RegistryQueryUtil.getBooleanValue("Ccay.GlobalParamters.EnablePushMessage")) {
			return;
		}
		// 数据字典查询选择器,若不配置,默认为null
		PushMessageVO messageVO = new PushMessageVO();
		messageVO.setMessageReceiver(String.valueOf(RequestContext.getCurrent().getUser().getUserId()));
		int totalSize =  pushMessageService.findNewMessageCount(messageVO,true);
		PushMessageVO message = new PushMessageVO();
		// 设置类型，便于在前台js接收判断此类型消息后进行处理
		message.setMessageType(messageType);
		// 取到我的代办任务的数据条数 由前台js对该值解析
		// 只要前台接收与此对应就行了，不一定使用MessageTitle
		message.setMessageTitle(String.valueOf(totalSize));
		// 设置选择器接收
		message.setMessageReceiver("#unReadMsgAccount");
		// !!!重要，应设置值为4，便于与系统级的pushMessage区分开
		message.setMessageFlag(4);
		// 设置返回的参数
		args.getMessageList().add(message);
	}

}
